<template>
  <div>
    <van-nav-bar title="个人信息" left-arrow @click-left="$router.back()" />
    <van-cell-group>
      <!-- 文件上传 hidden:隐藏  -->
      <input type="file" hidden ref="file" @change="onFileChange" />
      <!-- 点击头像 触发图片上传事件 -->
      <van-cell title="头像" is-link @click="$refs.file.click()">
        <van-image round width="30" height="30" fit="cover" :src="user.photo" />
      </van-cell>
      <van-cell
        title="昵称"
        :value="user.name"
        is-link
        @click="isUpdateNameShow = true"
      />
      <van-cell
        title="性别"
        :value="user.gender === 0 ? '男' : '女'"
        is-link
        @click="isUpdateGenderShow = true"
      />
      <van-cell
        title="生日"
        :value="user.birthday"
        is-link
        @click="isUpdateBirthdayShow = true"
      />
    </van-cell-group>

    <!-- 编辑昵称 弹层 -->
    <van-popup
      v-model="isUpdateNameShow"
      style="height: 100%"
      position="bottom"
    >
      <UpdateName
        v-model="user.name"
        @close="isUpdateNameShow = false"
        v-if="isUpdateNameShow"
      ></UpdateName>
    </van-popup>

    <!-- 编辑性别 弹层-->
    <van-popup
      v-model="isUpdateGenderShow"
      position="bottom"
      v-if="isUpdateGenderShow"
    >
      <UpdateGender
        v-model="user.gender"
        v-if="isUpdateGenderShow"
        @close="isUpdateGenderShow = false"
      ></UpdateGender>
    </van-popup>

    <!-- 编辑生日  弹层-->
    <van-popup
      v-model="isUpdateBirthdayShow"
      position="bottom"
      v-if="isUpdateBirthdayShow"
    >
      <UpdateBirthday
        @close="isUpdateBirthdayShow = false"
        v-model="user.birthday"
      ></UpdateBirthday>
    </van-popup>

    <!-- 编辑图像  弹层 -->
    <van-popup
      v-model="isUpdatePhotoShow"
      position="bottom"
      style="height: 100%"
      ><UpdatePhoto
        v-if="isUpdatePhotoShow"
        :img="img"
        @close="isUpdatePhotoShow = false"
        @update-photo = "user.photo = $event"
      >
      </UpdatePhoto
    ></van-popup>
  </div>
</template>

<script>
import { getUserProfile } from "@/api/user";
import UpdateName from "@/views/user-profile/components/update-name";
import UpdateGender from "@/views/user-profile/components/update-gender";
import UpdateBirthday from "@/views/user-profile/components/update-birthday";
import UpdatePhoto from "@/views/user-profile/components/update-photo";
import "cropperjs/dist/cropper.css";
import Cropper from "cropperjs";
export default {
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto,
  },
  data() {
    return {
      user: {},
      isUpdateNameShow: false, //修改昵称弹窗显示
      isUpdateGenderShow: false, // 控制性别弹框显示
      isUpdateBirthdayShow: false, //控制生日弹框显示
      isUpdatePhotoShow: false, //控制图片编辑弹窗显示
      img: null,
    };
  },
  created() {
    this.loadUserProfile();
  },
  methods: {
    async loadUserProfile() {
      try {
        const { data } = await getUserProfile();
        this.user = data.data;
      } catch (error) {
        this.$toast("获取用户个人信息失败");
        console.log(error);
      }
    },
    // 更新头像
    onFileChange() {
      // 获取文件对象
      const file = this.$refs.file.files[0]
      this.img = window.URL.createObjectURL(file)

       // 展示预览图片弹出层
      this.isUpdatePhotoShow = true

      // file-input 如果选了同一个文件不会触发 change 事件
      // 解决办法就是每次使用完毕，把它的 value 清空
      this.$refs.file.value = ''

    }
  },
};
</script>

<style scoped lang="less">
.van-popup {
  background-color: #f5f7f9;
}
.field-wrap {
  padding: 20px;
}
</style>
